import { createSlice } from "@reduxjs/toolkit";
import axios from "axios";

const channelStore = createSlice({
  name: "channel",
  // 初始化 state
  initialState: {
    channelList: [],
  },
  reducers: {
    setChannelList(state, action) {
      state.channelList = action.payload;
    },
  },
});

const { setChannelList } = channelStore.actions;
const reducer = channelStore.reducer;

function fetchChannelList() {
  return async (dispatch) => {
    console.log("fetch channel list");
    const res = await axios({
      url: "http://localhost:9000/channel/getChannelList",
      method: "POST",
    });
    console.log("fetch channel list, res", res);
    dispatch(setChannelList(res.data.data));
  };
}

export { setChannelList };
export { fetchChannelList };
export default reducer;
